package com.hongyuan.sellcoach.ui.home.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellcoach.base.CustomViewModel;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.MyRepository;
import com.hongyuan.sellcoach.ui.home.beans.StudentsBeans;
import com.hongyuan.sellcoach.ui.home.beans.StudentsBeans.ListBean;
import com.hongyuan.sellcoach.ui.home.beans.StudentsBeans.ListBean.CourseListBean;
import com.hongyuan.sellcoach.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;

public class StudentsViewMode extends CustomViewModel<MyRepository> {

    //数据集
    public ObservableField<List<MultiItemEntity>> mList = new ObservableField<>();

    //是否有所选学员数据
    public ObservableField<Boolean> haveSelect = new ObservableField<>();

    //当前是否搜索模式
    public ObservableField<Boolean> isSearch = new ObservableField<>();
    //输入的搜索内容
    public ObservableField<String> searchText = new ObservableField<>();

    public StudentsViewMode(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //显示数据
        public SingleLiveEvent<Void> showDatas = new SingleLiveEvent<>();
        //选中学员
        public SingleLiveEvent<Void> selectDatas = new SingleLiveEvent<>();
    }


    //预约学员
    public BindingCommand select = new BindingCommand(() -> {
        uc.selectDatas.call();
    });

    //点开搜索模式
    public BindingCommand openSearch = new BindingCommand(() -> {
        isSearch.set(true);
    });

    //关闭搜索模式
    public BindingCommand closeSearch = new BindingCommand(() -> {
        isSearch.set(false);
    });


    /*
     * 选中项
     * */
    public void clearSelect(CourseListBean courseListBean){
        for(MultiItemEntity entity : mList.get()){
            if(entity instanceof ListBean){
                ListBean listBean = (ListBean)entity;
                for(CourseListBean childBean : listBean.getCourse_list()){
                    if(childBean == courseListBean){

                        for(CourseListBean clearChildBean : listBean.getCourse_list()){

                            if(!clearChildBean.isSelect() && clearChildBean == courseListBean){
                                courseListBean.setSelect(true);
                            }else{
                                clearChildBean.setSelect(false);
                            }

                        }
                        return;
                    }
                }
            }
        }
    }

    /*
     * 组装选中的学员信息
     * */
    public String createStr(){
        List<ListBean> selectList = new ArrayList<>();

        for(MultiItemEntity entity : mList.get()){
            if(entity instanceof ListBean){
                ListBean listBean = (ListBean)entity;

                for(CourseListBean childBean : listBean.getCourse_list()){
                    if(childBean.isSelect()){
                        selectList.add(listBean);
                    }
                }
            }
        }

        if(selectList.size() > 0){
            return GsonUtil.toJsonStr(selectList);
        }
        return "";

    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.PRIVITE_USER_ORDER){
            StudentsBeans beans = (StudentsBeans)dataBean;

            if(curPage == FIRST_PAGE){
                mList.set(beans.getmList());
            }else{
                if(beans.getmList() != null && beans.getmList().size() > 0){
                    mList.get().addAll(beans.getmList());
                }else{
                    getUC().getFinishLoadMoreData().call();
                }
            }

            if(mList.get() != null && mList.get().size() > 0){
                //adapter.setNewData(mList);
                //打开所有子项列表
                //adapter.expandAll();
                uc.showDatas.call();
            }else{

            }

        }


    }
}
